package com.company.LambdaDemo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new Student(i, "测试" + i));
        }
        Optional<Student> name = list.stream().filter(a -> a.getId() == 13).findFirst();
        if (name.equals(Optional.empty())) {
            System.out.println("没查到");

        }
        System.out.println(name);
        list.forEach(item -> {
            if (item.getId() % 2 == 0) {
                return;
            }
            System.out.println(item.getId());
        });

        Map<Integer, Student> map = list.stream().collect(Collectors.toMap(Student::getId, Function.identity()));
        map.forEach((k, v) -> {
            v.setName(v.getName() + "测试修改字典");
        });
        map.computeIfAbsent(15, key -> {
            return new Student(key, "测试15" + key);
        });
        list.forEach(item -> {
            System.out.println(item.getName());
        });

        Map<Integer, Object> testMap = list.stream().collect(Collectors.toMap(Student::getId, Student::getName));
        System.out.println();
    }

    static String test(Function<Integer, String> dunc) {
        return dunc.apply(111);
    }

    private static ObjectResponse handle(String message, ExceptionHandle exceptionHandle) {
        ObjectResponse response = new ObjectResponse<>();
        try {
            response = exceptionHandle.handle();
            response.setStatus(200);
            response.setMessage("成功！");
        } catch (Exception e) {
            response.setData(false);
            response.setStatus(404);
            response.setMessage("失败！" + e.getMessage());
        }
        return response;
    }

    static ObjectResponse<Boolean> TestDemo() {
        ObjectResponse response;
        response = handle("测试", () -> {
            ObjectResponse<Boolean> objResponse = new ObjectResponse<>();
            objResponse.setData(true);
            // return objResponse;
            throw new Exception("有问题抛出异常！");
        });
        System.out.println(response);
        return response;
    }
}
